{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Closest Divisors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: closestDivisors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最接近的因数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数&nbsp;<code>num</code>，请你找出同时满足下面全部要求的两个整数：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>两数乘积等于 &nbsp;<code>num + 1</code>&nbsp;或&nbsp;<code>num + 2</code></li>\n",
    "\t<li>以绝对差进行度量，两数大小最接近</li>\n",
    "</ul>\n",
    "\n",
    "<p>你可以按任意顺序返回这两个整数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 8\n",
    "<strong>输出：</strong>[3,3]\n",
    "<strong>解释：</strong>对于 num + 1 = 9，最接近的两个因数是 3 &amp; 3；对于 num + 2 = 10, 最接近的两个因数是 2 &amp; 5，因此返回 3 &amp; 3 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 123\n",
    "<strong>输出：</strong>[5,25]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 999\n",
    "<strong>输出：</strong>[40,25]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num &lt;= 10^9</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [closest-divisors](https://leetcode.cn/problems/closest-divisors/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [closest-divisors](https://leetcode.cn/problems/closest-divisors/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['8', '123', '999']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        num1 = num + 1\n",
    "        cur1 = float('inf')\n",
    "        res1 = [0, 0]\n",
    "        for i in range(1, int(math.sqrt(num1)) + 1):\n",
    "            if num1 % i == 0:\n",
    "                j = num1 // i \n",
    "                cur1 = min(cur1, abs(i -j))\n",
    "                res1[0] = i\n",
    "                res1[1] = j\n",
    "        num2 = num + 2\n",
    "        cur2 = float('inf')\n",
    "        res2 = [0, 0]\n",
    "        for i in range(1, int(math.sqrt(num2)) + 1):\n",
    "            if num2 % i == 0:\n",
    "                j = num2 // i \n",
    "                cur2 = min(cur2, abs(i -j))\n",
    "                res2[0] = i\n",
    "                res2[1] = j\n",
    "        if cur1 < cur2:\n",
    "            return res1\n",
    "        else:\n",
    "            return res2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans_one = self.get_closet_divs(num+1)\n",
    "        ans_two =  self.get_closet_divs(num+2)\n",
    "        if abs(ans_one[0] - ans_one[1]) < abs(ans_two[0] - ans_two[1]):\n",
    "            return ans_one \n",
    "        else:\n",
    "            return ans_two\n",
    "\n",
    "\n",
    "    def get_closet_divs(self, num):\n",
    "        left = math.floor(math.sqrt(num))\n",
    "        ans = [1, num]\n",
    "        while (left >= 1):\n",
    "            if num % left == 0:\n",
    "                right = num // left \n",
    "                # try to update \n",
    "                if abs(left - right) < abs(ans[0] - ans[1]):\n",
    "                    ans = [left, right]\n",
    "            left -= 1 \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        num += 1\n",
    "        min = num\n",
    "        ans = []\n",
    "        for i in range(1, int(math.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                a = num // i\n",
    "                if (a - i) < min:\n",
    "                    min = a - i\n",
    "                    ans = [i, a]\n",
    "        num += 1\n",
    "        for i in range(1, int(math.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                a = num // i\n",
    "                if (a - i) < min:\n",
    "                    min = a - i\n",
    "                    ans = [i, a]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        '''\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n",
    "        '''\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == n1: return x, y\n",
    "            if x * (y := n2 // x) == n2: return x, y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def calculate(n):\n",
    "            for i in range(int(math.sqrt(n)), -1, -1):\n",
    "                if n % i == 0:\n",
    "                    return [i, n // i]\n",
    "\n",
    "        result = [1, float(\"inf\")]\n",
    "        for it in range(num+1, num+3):\n",
    "            cur = calculate(it)\n",
    "            if cur[1] - cur[0] < result[1] - result[0]:\n",
    "                result = cur\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def resolve(n):\n",
    "            for i in range(int(math.sqrt(n)), 0, -1):\n",
    "                if n % i == 0:\n",
    "                    return [i, n // i]\n",
    "\n",
    "        res1 = resolve(num + 1)\n",
    "        res2 = resolve(num + 2)\n",
    "\n",
    "        return res1 if abs(res1[0] - res1[1]) <= abs(res2[0] - res2[1]) else res2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def oneDivisor(num: int) -> List[int]:\n",
    "            for i in range(int(math.sqrt(num)), 0, -1):\n",
    "                if num % i == 0:\n",
    "                    return [i, num // i]\n",
    "        \n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = oneDivisor(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        res = inf\n",
    "        R = [inf,inf]\n",
    "        for v in num+1,num+2:\n",
    "            for i in range(1,100000):\n",
    "                if not v % i:\n",
    "                    temp = abs(v//i - i)\n",
    "                    if res > temp:\n",
    "                        res = temp\n",
    "                        R = [i,v//i]\n",
    "        return R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divide(self, n):\n",
    "        for i in range(int(math.sqrt(n)), 0, -1):\n",
    "            if n % i == 0:\n",
    "                return [i, n // i]\n",
    "\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = self.divide(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        a = num + 1\n",
    "        c = num + 2\n",
    "        b = int(math.sqrt(c))\n",
    "        while 1:\n",
    "            if a % b == 0:\n",
    "                return [b, a//b]\n",
    "            if c % b == 0:\n",
    "                return [b, c//b]\n",
    "            b -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        m =  int(num ** 0.5)+1\n",
    "        for x in range(m,0,-1):\n",
    "            if x * (y:=(num+1)//x)==num+1:\n",
    "                return x,y\n",
    "            if x * (y:=(num+2)//x)==num+2:\n",
    "                return x,y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        num_1,num_2=[],[]\n",
    "        for i in range(1,int((num+1)**0.5)+1):\n",
    "            if (num+1)%i==0:\n",
    "                num_1.append(i)\n",
    "        for i in range(1,int((num+2)**0.5)+1):\n",
    "            if (num+2)%i==0:\n",
    "                num_2.append(i)\n",
    "        n_1=int((num+1)/num_1[-1])-num_1[-1]\n",
    "        n_2=int((num+2)/num_2[-1])-num_2[-1]\n",
    "        if n_1<n_2:\n",
    "            return [num_1[-1],int((num+1)/num_1[-1])]\n",
    "        else:\n",
    "            return [num_2[-1],int((num+2)/num_2[-1])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans_one = self.get_closet_divs(num+1)\n",
    "        ans_two =  self.get_closet_divs(num+2)\n",
    "        if abs(ans_one[0] - ans_one[1]) < abs(ans_two[0] - ans_two[1]):\n",
    "            return ans_one \n",
    "        else:\n",
    "            return ans_two\n",
    "\n",
    "\n",
    "    def get_closet_divs(self, num):\n",
    "        left = math.floor(math.sqrt(num))\n",
    "        ans = [1, num]\n",
    "        while (left >= 1):\n",
    "            right = num // left \n",
    "            if num % left == 0:\n",
    "                # try to update \n",
    "                if abs(left - right) < abs(ans[0] - ans[1]):\n",
    "                    ans = [left, right]\n",
    "            left -= 1 \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        # a * b = num + 1\n",
    "        # a * (a + d) = num + 1\n",
    "        # a^2 + d*a - (num + 1) = 0\n",
    "        a = 2\n",
    "        res = [1, num + 1]\n",
    "        while a * a <= num + 2:\n",
    "            if (num + 1) % a == 0 and (num + 1) // a - a < res[1] - res[0]:\n",
    "                res = [a, (num + 1) // a]\n",
    "            elif (num + 2) % a == 0 and (num + 2) // a - a < res[1] - res[0]:\n",
    "                res = [a, (num + 2) // a]\n",
    "            a += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        '''\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n",
    "        '''\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == n1: return x, y\n",
    "            if x * (y := n2 // x) == n2: return x, y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divide(self, n):\n",
    "        for i in range(int(math.sqrt(n)), 0, -1):\n",
    "            if n % i == 0:\n",
    "                return [i, n // i]\n",
    "\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = self.divide(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divide(self, n):\n",
    "        for i in range(int(math.sqrt(n)), 0, -1):\n",
    "            if n % i == 0:\n",
    "                return [i, n // i]\n",
    "\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = self.divide(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divide(self, n):\n",
    "        for i in range(int(math.sqrt(n)), 0, -1):\n",
    "            if n % i == 0:\n",
    "                return [i, n // i]\n",
    "\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = self.divide(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        '''\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n",
    "        '''\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            #if x * (y := n1 // x) == n1: return x, y\n",
    "            #if x * (y := n2 // x) == n2: return x, y\n",
    "            if (y := n1 / x) == int(y): return x, int(y)\n",
    "            if (y := n2 / x) == int(y): return x, int(y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def closestDivisor(num) -> List[int]:\n",
    "            start = int(sqrt(num))\n",
    "            for i in range(start,0,-1):\n",
    "                if num%i == 0:\n",
    "                    return [i,num//i]\n",
    "            return [start,end]\n",
    "        ans1 = closestDivisor(num+1)\n",
    "        ans2 = closestDivisor(num+2)\n",
    "        print(ans1,ans2)\n",
    "        if ans1[1]-ans1[0] > ans2[1]-ans2[0]:\n",
    "            return ans2\n",
    "        return ans1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divide(self, n):\n",
    "        for i in range(int(math.sqrt(n)), 0, -1):\n",
    "            if n % i == 0:\n",
    "                return [i, n // i]\n",
    "\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = self.divide(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        t1 = num + 1\n",
    "        t2 = num + 2\n",
    "        ans1, ans2 = [], []\n",
    "        for i in range(int(math.sqrt(t1)), 0, -1):\n",
    "            if t1 % i == 0:\n",
    "                ans1.extend([i, t1 // i])\n",
    "                break\n",
    "\n",
    "        for i in range(int(math.sqrt(t2)), 0, -1):\n",
    "            if t2 % i == 0:\n",
    "                ans2.extend([i, t2 // i])\n",
    "                break\n",
    "        return min(ans1, ans2, key=lambda x: x[1] - x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == n1: return x, y\n",
    "            if x * (y := n2 // x) == n2: return x, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def get_divisors(x: int):\n",
    "            for i in range(int(math.sqrt(x)), 0, -1):\n",
    "                if x % i == 0:\n",
    "                    return [i, x // i]\n",
    "        res = [0, 10 ** 9]\n",
    "        for n in range(num + 1, num + 3):\n",
    "            cur = get_divisors(n)\n",
    "            if abs(cur[0] - cur[1]) < abs(res[0] - res[1]):\n",
    "                res = cur\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == n1: return x, y\n",
    "            if x * (y := n2 // x) == n2: return x, y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from math import sqrt\n",
    "class Solution:\n",
    "    def test(self,num:int,test1:int,test2:int) -> int:\n",
    "        for i in range(int(sqrt(num)+1), 1, -1):\n",
    "            if test1 % i == 0 or test2 % i == 0: return i, test1 % i, test1, test2\n",
    "    def closestDivisors(self, num: int) -> list[int]:\n",
    "        i, res, test1, test2 = Solution().test(num=num,test1=num+1,test2=num+2) \n",
    "        return [i,test1//i] if res == 0 else [i,test2//i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        a = num + 1\n",
    "        c = num + 2\n",
    "        b = int(math.sqrt(c))\n",
    "        while 1:\n",
    "            if a % b == 0:\n",
    "                return [b, a//b]\n",
    "            if c % b == 0:\n",
    "                return [b, c//b]\n",
    "            b -= 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def resolve(n):\n",
    "            for i in range(int(math.sqrt(n)), 0, -1):\n",
    "                if n % i == 0:\n",
    "                    return [i, n // i]\n",
    "\n",
    "        res1, res2 = resolve(num + 1), resolve(num + 2)\n",
    "        return res1 if abs(res1[0] - res1[1]) <= abs(res2[0] - res2[1]) else res2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def resolve(n):\n",
    "            for i in range(int(math.sqrt(n)), 0, -1):\n",
    "                if n % i == 0:\n",
    "                    return [i, n // i], abs(i - n // i)\n",
    "\n",
    "        res1, res2 = resolve(num + 1), resolve(num + 2)\n",
    "        return res1[0] if res1[1] <= res2[1] else res2[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divide(self, n):\n",
    "        for i in range(int(math.sqrt(n)), 0, -1):\n",
    "            if n % i == 0:\n",
    "                return [i, n // i]\n",
    "\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        ans = [0, int(1e9)]\n",
    "        for i in range(num + 1, num + 3):\n",
    "            cur = self.divide(i)\n",
    "            if abs(cur[0] - cur[1]) < abs(ans[0] - ans[1]):\n",
    "                ans = cur\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        num1 = num + 1\n",
    "        num2 = num + 2\n",
    "        l = []\n",
    "        for i in range(1, int(num1 ** 0.5 + 1)):\n",
    "            if num1 / i == num1 // i:\n",
    "                l += [[i, num1 // i, abs(i - num1 // i)]]\n",
    "        for i in range(1, int(num2 ** 0.5 + 1)):\n",
    "            if num2 / i == num2 // i:\n",
    "                l += [[i, num2 // i, abs(i - num2 // i)]]\n",
    "        def func3(l):\n",
    "            return l[2]\n",
    "        l.sort(key=func3)\n",
    "        return l[0][:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        m =  int(num ** 0.5)+1\n",
    "        for x in range(m,0,-1):\n",
    "            if x * (y:=(num+1)//x)==num+1:\n",
    "                return x,y\n",
    "            if x * (y:=(num+2)//x)==num+2:\n",
    "                return x,y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        '''\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n",
    "        '''\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == num + 1: return x, y\n",
    "            if x * (y := n2 // x) == num + 2: return x, y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def helper(x):\n",
    "            mid = isqrt(x)\n",
    "            while x % mid != 0:\n",
    "                mid -= 1\n",
    "            return  [mid, x // mid]\n",
    "        a = helper(num+1)\n",
    "        b = helper(num+2)\n",
    "        if abs(a[1] - a[0]) < abs(b[1] - b[0]):\n",
    "            return a \n",
    "        else:\n",
    "            return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        '''\n",
    "        for x in range(int((num + 2) ** 0.5), 0, -1):\n",
    "            y = (num + 1) // x\n",
    "            if x * y == num + 1: return x, y\n",
    "            y = (num + 2) // x\n",
    "            if x * y == num + 2: return x, y\n",
    "        '''\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == n1: return x, y\n",
    "            if x * (y := n2 // x) == n2: return x, y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        n1, n2 = num + 1, num + 2\n",
    "        for x in range(int(n2 ** 0.5), 0, -1):\n",
    "            if x * (y := n1 // x) == n1: return x, y\n",
    "            if x * (y := n2 // x) == n2: return x, y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) :\n",
    "\n",
    "        pos = int((num + 2) ** 0.5) + 1\n",
    "        result = []\n",
    "        cnt = 10 ** 10\n",
    "\n",
    "        for i in range(pos, 1 ,-1):\n",
    "            \n",
    "            if (num + 1) % i == 0:\n",
    "                a = (num+1)  // i\n",
    "                if abs(a-i) < cnt:\n",
    "                    cnt = abs(a-i)\n",
    "                    result = [a, i]\n",
    "            \n",
    "            if (num + 2) % i == 0:\n",
    "                a = (num + 2) // i\n",
    "                if abs(a-i) < cnt:\n",
    "                    cnt = abs(a-i)\n",
    "                    result = [a, i]\n",
    "\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def func(x):\n",
    "            for i in range(floor(sqrt(x)), 0, -1):\n",
    "                if x % i == 0:\n",
    "                    return [i, x//i]\n",
    "\n",
    "        a, b = func(num+1), func(num+2)\n",
    "        return a if abs(a[0]-a[1]) <= abs(b[0]-b[1]) else b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) :\n",
    "\n",
    "        pos = int((num + 2) ** 0.5) + 1\n",
    "        result = []\n",
    "        cnt = 10 ** 10\n",
    "\n",
    "        for i in range(pos, 1 ,-1):\n",
    "            \n",
    "            if (num + 1) % i == 0:\n",
    "                a = (num+1)  // i\n",
    "                if abs(a-i) < cnt:\n",
    "                    cnt = abs(a-i)\n",
    "                    result = [a, i]\n",
    "            \n",
    "            if (num + 2) % i == 0:\n",
    "                a = (num + 2) // i\n",
    "                if abs(a-i) < cnt:\n",
    "                    cnt = abs(a-i)\n",
    "                    result = [a, i]\n",
    "\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def closestDivisor(num) -> List[int]:\n",
    "            start = int(sqrt(num))\n",
    "            for i in range(start,0,-1):\n",
    "                if num%i == 0:\n",
    "                    return [i,num//i]\n",
    "            return [start,end]\n",
    "        ans1 = closestDivisor(num+1)\n",
    "        ans2 = closestDivisor(num+2)\n",
    "        #print(ans1,ans2)\n",
    "        if ans1[1]-ans1[0] > ans2[1]-ans2[0]:\n",
    "            return ans2\n",
    "        return ans1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        t = []\n",
    "        i = 1\n",
    "        while i * i <= num + 2:\n",
    "            if (num + 1) % i == 0:\n",
    "                t.append((i, (num + 1) // i))\n",
    "            if (num + 2) % i == 0:\n",
    "                t.append((i, (num + 2) // i))\n",
    "            i += 1\n",
    "        return min(t, key = lambda x: abs(x[0]-x[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closestDivisors(self, num: int) -> List[int]:\n",
    "        def find(num):\n",
    "            i = int(pow(num, 0.5))\n",
    "            while num % i != 0:\n",
    "                i -= 1\n",
    "            return i, int(num/i)\n",
    "        a1, b1 = find(num + 1)\n",
    "        a2, b2 = find(num + 2)\n",
    "        if abs(a1 - b1) < abs(a2 - b2):\n",
    "            return [a1, b1]\n",
    "        else:\n",
    "            return [a2, b2]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from math import sqrt\n",
    "class Solution:\n",
    "    def test(self,num:int,test1:int,test2:int) -> int:\n",
    "        for i in range(int(sqrt(num)+1), 1, -1):\n",
    "            if test1 % i == 0 or test2 % i == 0: return i, test1 % i, test1, test2\n",
    "    def closestDivisors(self, num: int) -> list[int]:\n",
    "        i, res, test1, test2 = Solution().test(num=num,test1=num+1,test2=num+2) \n",
    "        return [i,test1//i] if res == 0 else [i,test2//i]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
